﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using NPOI.HSSF.Model;
using System.IO;
using System.Collections;

namespace WXYW
{
    public partial class AddSparePart : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //using (FileStream fs = File.Open(@"C:\\test.xls", FileMode.Open))
                //{
                //    NPOI.HSSF.UserModel.HSSFWorkbook hb = new NPOI.HSSF.UserModel.HSSFWorkbook(fs);
                    
                //    NPOI.SS.UserModel.ISheet sheet = hb.GetSheetAt(0);

                //    IEnumerator rows = sheet.GetRowEnumerator();
                //}


                managerLbl.Text = ContextService.CurrentAccount.UserName;

                ServiceStationService serviceStationService = new ServiceStationService();
                enterServiceStationDropDownList.DataSource = serviceStationService.GetALLServiceStations();
                enterServiceStationDropDownList.DataBind();

                //ApplicationProductService applicationProductService = new ApplicationProductService();
                //productTypeDropdownList.DataSource = applicationProductService.GetAllProductType();
                //productTypeDropdownList.DataTextField = "ProductTypeName";
                //productTypeDropdownList.DataValueField = "ProductTypeID";
                ProductClassService classService = new ProductClassService();
                productTypeDropdownList.DataSource = classService.GetAllClass();
                productTypeDropdownList.DataTextField = "Name";
                productTypeDropdownList.DataValueField = "ID";
                productTypeDropdownList.DataBind();
            }
        }

        protected void saveNewBtn_Click(object sender, EventArgs e)
        {
            try
            {
                AddNewSparePart();
                messageLbl.Text = string.Format("{0} 已成功添加，请添加下一条记录。", wlTxtBox.Text.Trim());
                Clear();
            }
            catch (Exception ex)
            {
            }
        }

        
        private void AddNewSparePart()
        {
            string materialNO = wlTxtBox.Text.Trim();
            string modelNumber = modelNOTxtBox.Text.Trim();
            string versionNumber = versionNOTxtBox.Text.Trim();
            string stockType = storageTypeDropdownList.SelectedValue;
            string marketPrice = marketPriceTxtBox.Text.Trim();
            string sparePartPrice = sparePartPriceTxtBox.Text.Trim();
            string status = productStatusDropdownList.SelectedValue;
            int enterStationID = Convert.ToInt32(enterServiceStationDropDownList.SelectedValue);
            int count = Convert.ToInt32(countTxtBox.Text.ToString());
            string stockPlace = kwTxtBox.Text.Trim();

            SparePartService sparePartService = new SparePartService();
            sparePartService.AddSparePart(materialNO, Convert.ToInt32(productTypeDropdownList.SelectedValue), modelNumber, versionNumber, count, stockType, marketPrice, sparePartPrice,
                status, enterStationID, ContextService.CurrentAccount, stockPlace, rkNOTxtBox.Text.Trim(), rkComment.Text.Trim());
        }

        private void Clear()
        {
            wlTxtBox.Text = string.Empty;
            modelNOTxtBox.Text = string.Empty;
            marketPriceTxtBox.Text = string.Empty;
            sparePartPriceTxtBox.Text = string.Empty;
            versionNOTxtBox.Text = string.Empty;
            countTxtBox.Text = string.Empty;
            rkNOTxtBox.Text = string.Empty;
            rkComment.Text = string.Empty;
        }
    }
}
